﻿

/// <reference path="ol3/ol-whitespace.js" />

goog.provide('ol.source.LiuG');


goog.require('ol.Attribution');
goog.require('ol.source.XYZ');



/**
 * @constructor
 * @extends {ol.source.XYZ}
 * @param {olx.source.OSMOptions=} opt_options Open Street Map options.
 * @param  改写osm的瓦片获取方式，实现加载geowebcache提供的瓦片数据
 * @todo api
 */
ol.source.LiuG = function (opt_options)
{

    var options = goog.isDef(opt_options) ? opt_options : {};

    var attributions;
    if (goog.isDef(options.attributions))
    {
        attributions = options.attributions;
    } else
    {
        attributions = ol.source.LiuG.ATTRIBUTIONS;
    }

    this.layerName = "";
    if (goog.isDef(options.params.layers))
    {
        this.layerName = options.params.layers;
    }

    this.format = "image/png";
    if (goog.isDef(options.params.format))
    {
        this.format = options.params.format;
    }


    var crossOrigin = goog.isDef(options.crossOrigin) ?
        options.crossOrigin : 'anonymous';

    var protocol = ol.IS_HTTPS ? 'https:' : 'http:';
    var url = goog.isDef(options.url) ?
        options.url : protocol + '//{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png';

    goog.base(this, {
        attributions: attributions,
        crossOrigin: crossOrigin,
        opaque: true,
        maxZoom: options.maxZoom,
        tileLoadFunction: options.tileLoadFunction,
        url: url
    });

};
goog.inherits(ol.source.LiuG, ol.source.XYZ);


ol.source.LiuG.prototype.getTile =
    function (z, x, y, pixelRatio, projection)
    {
        var tileCoordKey = this.getKeyZXY(z, x, y);
        if (this.tileCache.containsKey(tileCoordKey))
        {
            return /** @type {!ol.Tile} */ (this.tileCache.get(tileCoordKey));
        } else
        {
            //这里有好大一部分代码是没有用的

            goog.asserts.assert(projection);
            var tileCoord = new ol.TileCoord(z, x, y);
            var tileUrl = this.tileUrlFunction(tileCoord, pixelRatio, projection);
            var tile = new this.tileClass(
                tileCoord,
                goog.isDef(tileUrl) ? ol.TileState.IDLE : ol.TileState.EMPTY,
                goog.isDef(tileUrl) ? tileUrl : '',
                this.crossOrigin,
                this.tileLoadFunction);
            this.tileCache.set(tileCoordKey, tile);

            var t = Math.pow(2, z);

            x = (x % t);
            y = ((y+1)% t);
            x = x < 0 ? t+x : x;
            y = y < 0 ? -y : y;

            var tempUrl = tileUrl + "?layers=" + this.layerName + "&zoom=" + z + "&x=" +x + "&y=" + y + "&format="+this.format;

            tile.src_ = tempUrl;


            return tile;
        }
    };





/**
 * @const
 * @type {ol.Attribution}
 * @todo api
 */
ol.source.LiuG.DATA_ATTRIBUTION = new ol.Attribution({
    html: 'Data &copy; ' +
        '<a href="http://www.openstreetmap.org/">OpenStreetMap</a> ' +
        'contributors, ' +
        '<a href="http://www.openstreetmap.org/copyright">ODbL</a>'
});


/**
 * @const
 * @type {ol.Attribution}
 * @todo api
 */
ol.source.LiuG.TILE_ATTRIBUTION = new ol.Attribution({
    html: 'Tiles &copy; ' +
        '<a href="http://www.openstreetmap.org/">OpenStreetMap</a> ' +
        'contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>'
});


/**
 * @const
 * @type {Array.<ol.Attribution>}
 */
ol.source.LiuG.ATTRIBUTIONS = [
  ol.source.LiuG.TILE_ATTRIBUTION,
  ol.source.LiuG.DATA_ATTRIBUTION
];
